﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CoreLib
{
    /// <summary>
    /// 读取文件
    /// </summary>
    public class TrimbleFileConvertClass
    {
        /// <summary>
        ///读取CSV格式源文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static List<string> ConvertFromASC(string fileName)
        {
            List<string> rtn = new List<string>();

            FileStream aFile = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            string strLine = sr.ReadLine();

            while (strLine !=null)
            {
                string row = strLine;
                //分割字符串
                string[] tmpRow = System.Text.RegularExpressions.Regex.Split(row.Trim(), @"\s+");

                //条件限定
                if (tmpRow == null || tmpRow.Length < 4)
                {
                    strLine = sr.ReadLine();
                    continue;
                }

                if ((!tmpRow[1].Contains("°")) || (!tmpRow[1].Contains("’")) || (!tmpRow[1].Contains("”")))
                {
                    strLine = sr.ReadLine();
                    continue;
                }
                if ((!tmpRow[2].Contains("°")) || (!tmpRow[2].Contains("’")) || (!tmpRow[2].Contains("”")))
                {
                    strLine = sr.ReadLine();
                    continue;
                }

                string dh = tmpRow[0];//点号
                string wd = tmpRow[1];//维度
                string jd = tmpRow[2];//精度
                string ddg = tmpRow[3];//大地高

                try
                {
                    Convert.ToDouble(ddg);
                }
                catch
                {
                    strLine = sr.ReadLine();
                    continue;
                }

                string[] splitWd = wd.Split(new char[] { '°', '’', '.', '”' }, StringSplitOptions.RemoveEmptyEntries);
                if (splitWd.Length != 4)
                {
                    strLine = sr.ReadLine();
                    continue;
                }
                if (splitWd[2].Length != 2)
                {
                    splitWd[2] = "0" + splitWd[2];
                }
                tmpRow[1] = splitWd[0] + "." + splitWd[1] + splitWd[2] + splitWd[3];

                string[] splitJd = jd.Split(new char[] { '°', '’', '.', '”' }, StringSplitOptions.RemoveEmptyEntries);
                if (splitJd.Length != 4)
                {
                    strLine = sr.ReadLine();
                    continue;
                }
                if (splitJd[2].Length != 2)
                {
                    splitJd[2] = "0" + splitJd[2];
                }
                tmpRow[2] = splitJd[0] + "." + splitJd[1] + splitJd[2] + splitJd[3];

                tmpRow[0] = dh;
                tmpRow[3] = ddg;

                string tmp = tmpRow[0] + "\t\t" + tmpRow[1] + "\t\t" + tmpRow[2] + "\t\t" + tmpRow[3];
                rtn.Add(tmp);

                strLine = sr.ReadLine();
            }
            return rtn;
        }

        /// <summary>
        /// 读取Text格式源文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string[] ReadTxtData(string fileName)
        {
            string[] rtn = new string[2];

            return rtn;
        }
    }
}
